/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/5/22
 * Time: 23:07
 */

#include "Collider.h"

#include <glm/geometric.hpp>

Collider * Collider::AddColliderChild(ObjectScreen * _parent, glm::vec2 _size, Type _type, Anchor _anchor)
{
    auto collider = new Collider();
    collider->Init();
    collider->SetAnchor(_anchor);
    collider->SetParent(_parent);
    collider->SetSize(_size);
    collider->SetType(_type);
    _parent->AddChild(collider);

    return collider;
}

void Collider::Render()
{
#if 1
    ObjectAffiliate::Render();
    auto pos = parent_->GetRenderPosition() + offset_;
    game_.RenderFillCircle(pos, size_, 0.3);
#endif
    ObjectAffiliate::Render();
}

bool Collider::IsColliding(Collider * other)
{
    if(!other)
        return false;
    if(type_ == CIRCLE && other->type_ == CIRCLE)// 两个圆的情况
    {
        auto point1 = parent_->GetPosition() + offset_ + size_ / 2.0f;
        auto point2 = other->parent_->GetPosition() + other->offset_ + other->size_ / 2.0f;
        return glm::length(point1 - point2) < (size_.x + other->size_.x) / 2.0f;
    }
    // TODO： 其它形状的碰撞检测
    return false;
}
